package com.xxl.job.admin.util;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * @author hujj
 * @version 1.0
 * @date 2021/6/2/002 14:01
 */
@Slf4j
public class EnvUtil {
//    org.slf4j.Logger log = Logg
    private static String profile;
    public static boolean ifApplication; //是否开发用Application启动
    private static String configLocation;

    public static void setProfile(String profile) {
        EnvUtil.profile = profile;
    }

    //是否启用数据库配置参数（更多连接数）
    public static boolean useDbConfigParam() {
        if(profile.equals("test") || profile.equals("pro") || profile.equals("yfb"))
            return true;
        //开发模式，只给固定的1~3个连接
        return false;
    }

    public static void setConfigLocation(String configLocation) {
        EnvUtil.configLocation = configLocation;
    }
    public static String getStaticConfigLocation() {
        if(configLocation==null) {
            return "classpath:";
        }
        return configLocation;
    }

    /**
     * 启动立即可以读取的profile
     * @return
     */
    public static String getStaticProfile() {
        if(profile==null) {
            return "dev";
        }
        return profile;
    }

    /**
     * 是否本机开发环境，local、corp都是本机开发
     * @return
     */
    public static boolean enableTokenValidate() {
        String profile = getActiveProfile();
        //return "local".equals(profile) || "corp".equals(profile) || "test".equals(profile) || "hu".equals(profile);
        //本地、测试环境、我个人环境，开启真实token校验，即支持多用户注册、登录
        return "local".equals(profile) || "test".equals(profile) || "hu".equals(profile);
    }

    public static Environment getEnvironment() {
        return ApplicationContextHelper.getApplicationContext().getEnvironment();
    }

    public static String getConfigProperty(String key, String defaultValue) {
        Environment env = getEnvironment();
        return env.getProperty(key, defaultValue);
    }

    /**
     * 运行期读取的profile
     * @return
     */
    public static String getActiveProfile() {
        return getEnvironment().getActiveProfiles()[0];
    }

    /**
     * 获取系统配置文件路径，在启动命令行设置的
     * @return
     */
    public static String getConfigLocation() {
        return getEnvironment().getProperty("spring.config.location");
    }

    public static File getConfigFile(String filePath) throws IOException {
        File file = new File(filePath);
        if(file.exists() && file.isFile()) {
            return file;
        }
        try {
            filePath = filePath.trim();
            if(filePath.startsWith("/"))
                filePath = filePath.substring(1);
            file = new ClassPathResource(filePath).getFile();
            if (file.exists() && file.isFile()) {
                return file;
            }
        } catch (Throwable t) {
//            log.error("getConfigFile11 error, filePath: {}", filePath);
        }
        String configLocation = EnvUtil.getStaticConfigLocation();
        if(configLocation==null) {
            return null;
        }
        configLocation = configLocation.trim();
        String clsPathPrefix = "classpath:";
        if(configLocation.indexOf(clsPathPrefix)==0) {
            configLocation = configLocation.substring(clsPathPrefix.length());
            String configFile = configLocation+filePath;
//            log.info("getConfigFile22 configFile: {}, configLocation: {}", configFile, configLocation);
            try {
                file = new ClassPathResource(configFile).getFile();
                if (file.exists() && file.isFile()) {
                    return file;
                }
            } catch (Throwable t) {
                file = new ClassPathResource(configLocation).getFile();
                if (file.exists() && file.isFile()) {
                    return file;
                }
            }
//            log.error("getConfigFile33 error, configFile: {}", configFile);
            return null;
        } else {
            String configFile = null;
            if(configLocation.endsWith("/")&&filePath.startsWith("/"))
                configFile = configLocation+filePath.substring(1);
            else
                configFile = configLocation+filePath;
//            log.info("getConfigFile44 configFile: {}", configFile);
            return new File(configFile);
        }
    }

    public static boolean isDevMode() {
        Environment env = getEnvironment();
        String property = env.getProperty("login.mock");
        if(property==null || "0".equals(property))
            return false;
        return true;
    }
}